import {
    SearchMemberList,
    FindTagList,
    FindAllTagList,
    FindCardList,
    FindMemberInfoById,
    AddMemberTags,
    ChangePoints,
    AddCard,
    DelMemberTag,
    ChangeRemark
} from '@/services/customer/customerList';
import {catchException} from '@/utils/utils';

export default {
    namespace: 'customerList',

    state: {
        customerListInfo:{
            customerList:[],
            total:0,
            pageIndex:1,
            pageSize:10
        },        
        tagList:[],
        allTagList:[],
        cardList:[],
        memberInfo:{}
    },
    effects: {
        *SearchMemberList({ payload, callback }, { call, put }) {           
          const data = yield call(SearchMemberList, payload);
          yield catchException(function* (){
            if (data.Code === 0) {
                yield put({
                    type: 'saveSearchMemberList',
                    payload: {
                        list:data.Data.DataList,
                        total:data.Data.Total,
                        pageIndex:payload.PageIndex,
                        pageSize:payload.PageSize
                    },
                });
                callback && callback.success && callback.success(data);
              }else{
                callback && callback.error && callback.error(index,data);
              }      
          })        
        },             
        *FindTagList({ payload, callback }, { call, put }) {           
            const data = yield call(FindTagList, payload);
            yield catchException(function* (){
                if (data.Code === 0) {
                    yield put({
                        type: 'saveFindTagList',
                        payload: data.Data,
                    });
                    callback && callback.success && callback.success(data);
                  }else{
                    callback && callback.error && callback.error(index,data);
                  }     
            })            
        },  
        *FindAllTagList({ payload, callback }, { call, put }) {           
            const data = yield call(FindAllTagList, payload);
            yield catchException(function* (){
                if (data.Code === 0) {
                    yield put({
                        type: 'saveFindAllTagList',
                        payload: data.Data,
                    });
                    callback && callback.success && callback.success(data);
                }else{
                    callback && callback.error && callback.error(index,data);
                }    
            })         
        },  
        *FindCardList({ payload, callback }, { call, put }) {           
            const data = yield call(FindCardList, payload);
            yield catchException(function* (){
                if (data.Code === 0) {
                    yield put({
                        type: 'saveFindCardList',
                        payload: data.Data.Data||[],
                    });
                    callback && callback.success && callback.success(data);
                }else{
                    callback && callback.error && callback.error(index,data);
                }   
            })            
        },  
        *FindMemberInfoById({ payload, callback }, { call, put }) {           
            const data = yield call(FindMemberInfoById, payload);
            yield catchException(function* (){
                if (data.Code === 0) {
                    yield put({
                        type: 'saveFindMemberInfoById',
                        payload: {
                            list:data.Data.DataList,
                            total:data.Data.Total,
                            pageIndex:payload.PageIndex,
                            pageSize:payload.PageSize
                        },
                    });
                    callback && callback.success && callback.success(data);
                }else{
                    callback && callback.error && callback.error(index,data);
                }   
            })            
        },  
        *AddMemberTags({ payload, callback }, { call, put }) {           
            const data = yield call(AddMemberTags, payload);
            yield catchException(function* (){
                if (data.Code === 0) {
                //   yield put({
                //       type: 'saveAddMemberTags',
                //       payload: {
                //           list:data.Data.DataList,
                //           total:data.Data.Total,
                //           pageIndex:payload.PageIndex,
                //           pageSize:payload.PageSize
                //       },
                //   });
                    callback && callback.success && callback.success(data);
                }else{
                    callback && callback.error && callback.error(index,data);
                }   
            })             
        },  
        *ChangePoints({ payload, callback }, { call, put }) {           
            const data = yield call(ChangePoints, payload);
            yield catchException(function* (){
                if (data.Code === 0) {
                //   yield put({
                //       type: 'saveChangePoints',
                //       payload: {
                //           list:data.Data.DataList,
                //           total:data.Data.Total,
                //           pageIndex:payload.PageIndex,
                //           pageSize:payload.PageSize
                //       },
                //   });
                    callback && callback.success && callback.success(data);
                }else{
                    callback && callback.error && callback.error(index,data);
                }   
            })            
        },  
        *AddCard({ payload, callback }, { call, put }) {           
            const data = yield call(AddCard, payload);
            yield catchException(function* (){
                if (data.Code === 0) {
                //   yield put({
                //       type: 'saveAddCard',
                //       payload: {
                //           list:data.Data.DataList,
                //           total:data.Data.Total,
                //           pageIndex:payload.PageIndex,
                //           pageSize:payload.PageSize
                //       },
                //   });
                    callback && callback.success && callback.success(data);
                }else{
                    callback && callback.error && callback.error(index,data);
                }  
            })              
        },  
        *DelMemberTag({ payload, callback }, { call, put }) {           
            const data = yield call(DelMemberTag, payload);
            yield catchException(function* (){
                if (data.Code === 0) {
                //   yield put({
                //       type: 'saveDelMemberTag',
                //       payload: {
                //           list:data.Data.DataList,
                //           total:data.Data.Total,
                //           pageIndex:payload.PageIndex,
                //           pageSize:payload.PageSize
                //       },
                //   });
                    callback && callback.success && callback.success(data);
                }else{
                    callback && callback.error && callback.error(index,data);
                }  
            })
            
        },
        
        //修改备注
        *ChangeRemark({ payload, callback }, { call, put }) {           
            const data = yield call(ChangeRemark, payload);
            yield catchException(function* (){
                if (data.Code === 0) {
                    callback && callback.success && callback.success(data);
                }else{
                    callback && callback.error && callback.error(index,data);
                } 
            })                
        }, 
    },
    reducers: {
        saveSearchMemberList(state, { payload }) {            
            return {
                ...state,               
                customerListInfo:{
                    customerList: payload.list,
                    total:payload.total,
                    pageIndex:payload.pageIndex,
                    pageSize:payload.pageSize
                }
            };
        },
        changeCustomerList(state, { payload }) {            
            return {
                ...state,               
                customerListInfo:{
                    ...state.customerListInfo,
                    customerList: payload,
                    
                }
            };
        },
        saveFindTagList(state, { payload }) {            
            return {
                ...state,               
                tagList:payload
            };
        },  
        saveFindAllTagList(state, { payload }) {            
            return {
                ...state,               
                allTagList:payload
            };
        },  
        saveFindCardList(state, { payload }) {            
            return {
                ...state,               
                cardList:payload
            };
        },  
        saveFindMemberInfoById(state, { payload }) {            
            return {
                ...state,               
                memberInfo:payload
            };
        },                            
    },
}